"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const koa_router_1 = __importDefault(require("koa-router"));
const TileUtil4326_1 = __importDefault(require("../lib/TileTool/TileUtil4326"));
const TileUtil3857_1 = __importDefault(require("../lib/TileTool/TileUtil3857"));
const getFilePathList_1 = __importDefault(require("../lib/FSTool/getFilePathList"));
const configTool_1 = __importDefault(require("../com/configTool"));
const TileRoutes = new koa_router_1.default({ prefix: '/TileServer' });
const tileUtil3857 = new TileUtil3857_1.default();
const tileUtil4326 = new TileUtil4326_1.default();
TileRoutes
    .all('/getTilesByGeoJson', async (ctx) => {
    ctx.set('Content-Type', 'application/json;charset=utf-8');
    const { token, tileSchema, level, dataSourceName, dataSourceToken, targetDirectory, geoJson } = ctx.request.body;
    const tileUtil = tileSchema === 'layer-xyz-3857' ? tileUtil3857 : tileUtil4326;
    const tileList = tileUtil.getTilesByGeoJson(geoJson, level);
    ctx.body = JSON.stringify({
        code: 200,
        message: '瓦片分析成功',
        data: tileList
    });
    ctx.status = 200;
})
    .get('/getTileSetList', async (ctx) => {
    ctx.set('Content-Type', 'application/json;charset=utf-8');
    let fileList = await (0, getFilePathList_1.default)(configTool_1.default.wwwPath).catch((err) => {
        return [];
    });
    fileList = fileList.filter((item) => {
        return item.indexOf('tileset.json') > -1;
    });
    fileList = fileList.sort();
    ctx.body = fileList.map((item) => {
        const url = item.replace(configTool_1.default.wwwPath, '').replaceAll('\\', '/');
        const md5 = require('crypto').createHash('md5').update(item).digest('hex');
        return {
            pid: md5,
            name: item.split('\\')[item.split('\\').length - 2],
            url: url,
            diskPath: item
        };
    }).filter((item) => {
        return item.url.split('/').length === 3;
    });
});
exports.default = TileRoutes;
